/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao.blog.controller;

import com.altuure.yagdao.SearchResultList;
import com.altuure.yagdao.YPage;
import com.altuure.yagdao.blog.service.AdminService;
import com.altuure.yagdao.blog.service.BrowserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
@Controller
public class AdminController {


    protected final Log log = LogFactory.getLog(BrowseController.class);
    private AdminService adminService = null;

    @RequestMapping("/admin/users/{page}")
    public ModelAndView users(@PathVariable("page") int page) {
        final ModelAndView view = new ModelAndView("/admin/users");
        view.addObject("users", adminService.getUsers(new YPage(page * 10, 10)));
        return view;
    }

    @RequestMapping("/admin/users")
    public ModelAndView users() {
        return users(0);
    }

    @RequestMapping("/admin/posts/{page}")
    public ModelAndView posts(@PathVariable("page")int page,@RequestParam("o") String order) {
        final ModelAndView view = new ModelAndView("/admin/posts");
        final SearchResultList posts = adminService.getPosts(new YPage(order,true,page * 10, 10));
        view.addObject("posts", posts);
        view.addObject("postCount", posts.getPageCount());
        view.addObject("page", page);
        view.addObject("order", (order==null)?"id":order);
        return view;

    }

    @RequestMapping("/admin/posts")
    public ModelAndView posts() {
        return posts(0,null);
    }

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }
}
